<?php

namespace App\Admin\Controllers;

use App\Models\QuotationSheet;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;

class QuotationSheetController extends Controller
{
    use HasResourceActions;

    //报价列表
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('报价列表');
            $content->body($this->grid());
        });
    }

    //编辑报价
    public function edit($id, Content $content)
    {
        return $content->header('报价')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑报价
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //查看报价详情
    public function show($id, Content $content)
    {
        return $content
            ->header('报价信息')
            ->description('详情')
            ->body($this->detail($id));
    }

    //添加报价
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建报价');
            $content->body($this->form());
        });
    }


    //添加报价保存回调
    public function store()
    {
        return $this->form()->store();
    }

    //获取报价
    protected function grid()
    {
        return Admin::grid(QuotationSheet::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->title('标题')->style('max-width:200px;word-break:break-all;');
            $grid->desc('排序');
            $grid->is_show('是否显示')->display(function ($value) {
                return ($value==1) ? '是' : '否';
            });
            $grid->create_at('创建时间');

            $grid->model()->orderBy('id', 'desc');

            $grid->disableRowSelector();

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('title', '标题');
                });
            });

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(QuotationSheet::class, function (Form $form) {
            $form->text('title', '标题')->attribute(['maxlength' => '50'])->rules('required|max:50');
            $form->multipleImage('content', '报价内容')->removable()->rules('image');
            $form->number('desc', '排序')->default('0')->rules('required');
            $form->radio('is_show', '是否显示')->options([1 => '是', 0 => '否'])->default(1)->rules('required');
            $form->image('background_image', '背景图片')->rules('required')->rules('image')->removable();
            $form->hidden('create_at')->default(date('Y-m-d H:i:s', time()));

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }

    /**
     * 报价详情
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(QuotationSheet::findOrFail($id));
        $show->id('ID');
        $show->title('标题');
        $show->content('报价内容')->unescape()->as(function ($value) {
            $img = '';
            foreach ($value as $k => $v) {
                $img .= "<img src='{$v}' style='width: 200px;height: 200px;margin-left: 10px;'/>";
            }
            return $img;
        });
        $show->desc('排序');
        $show->is_show('是否显示')->using([0 => '否', 1 => '是']);

        $show->background_image('背景图片')->unescape()->as(function ($value) {
            if(empty($value)) {
                $img = '';
            }else{
                $img = "<img src='{$value}' style='width: 200px;height: 200px;margin-left: 10px;'/>";
            }
            return $img;
        });

        $show->create_at('创建时间');

        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });

        return $show;
    }
}
